<?php
namespace App\Http\Controllers;
use Log;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Illuminate\Http\Request;

class WechatController extends Controller
{


    /*
     * js获取微信的信息
     */
    public function getWechatInfo()
    {
//        $res = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx9d1e451eab846f3d&secret=4cad6f43f1b2fb2cfd90e908185c0c4e');
//        $res = json_decode($res, true);
//        dd($res);
        //$token = $res['access_token'];
        $token = 'aBjrUrpTjhuDZDTJudBaKnd-WSxgeZmgGnapKSBRjZDM6DuXpqMVBX_zfW0lGCEhfMB6LjUORp87XT8gcNyCF_fwBCy0RV1gIlquGT1yHmcucq6zjjSPvxfechmIeVRvYCYhADAOSO';
        $url2 = sprintf("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi", $token);
        $re = file_get_contents($url2);
        $re = json_decode($re, true);
        $timestamp = time();
        $nonceStr = $this->createNonceStr();
        $ticket = $re['ticket'];
        $url = 'http://zcai.cn:8043/htmlPage/test.html';
        $wxOri = "jsapi_ticket=$ticket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
        $wxSha1 = sha1($wxOri);
        $result['appId'] = 'wx9d1e451eab846f3d';
        $result['nonceStr'] = $nonceStr;
        $result['timestamp'] = $timestamp;
        $result['url'] = $url;
        $result['signature'] = $wxSha1;
        $result['rawString'] = $wxOri;
        return response()->json(['msg' => 1, 'data' => $result]);
    }
    public function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }



}